package com.jxtc.enterprise.tenant.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxtc.enterprise.common.aspectj.TokenRequired;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.common.vo.PaginationResponseVO;
import com.jxtc.enterprise.tenant.dto.TenantChangeFoodSupplyStateDTO;
import com.jxtc.enterprise.tenant.dto.TenantFoodCreateDTO;
import com.jxtc.enterprise.tenant.dto.TenantFoodUpdateDTO;
import com.jxtc.enterprise.tenant.service.TenantFoodManagementService;
import com.jxtc.enterprise.tenant.vo.TenantFoodCreateVO;
import com.jxtc.enterprise.tenant.vo.TenantFoodDetailVO;
import com.jxtc.enterprise.tenant.vo.TenantFoodVO;
import com.jxtc.enterprise.tenant.vo.TenantStoreWithFoodTypeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/jxtc/enterprise/tenant/foodManage")
@RequiredArgsConstructor
@Slf4j
public class TenantFoodManagementController {

    private final TenantFoodManagementService tenantFoodManagementService;


    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示添加菜品成功，此时 data 字段返回新增餐品的相关信息（餐品 ID 和餐品名称）")
    @ApiResponse(responseCode = "200", description = "code 为 400 时， 表示添加菜品失败，此时 message 字段为错误提示信息，data 字段为空")
    @Operation(summary = "2.5.1 租户新增菜品接口", tags = {"2.5 租户菜品管理"}, operationId = "2.5.1")
    @TokenRequired(role = TokenRequired.ROLE_TENANT)
    @PostMapping("/addFood")
    public Result<TenantFoodCreateVO> addFood(@Valid @RequestBody TenantFoodCreateDTO dto) {
        log.info("/addFood, request: {}", JSONObject.toJSONString(dto));
        TenantFoodCreateVO vo = tenantFoodManagementService.saveFood(dto);

        log.info("/addFood, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }


    @ApiResponse(responseCode = "200", description = "code 为 200 时，data 字段为包含菜品列表的分页信息")
    @Parameter(name = "size", description = "每页显示的记录数，默认为 10")
    @Parameter(name = "page", description = "当前页码，从 1（默认值）开始")
    @Parameter(name = "storeId", description = "店铺 ID")
    @Parameter(name = "keyword", description = "查询关键字，根据菜品名称模糊查询")
    @Parameter(name = "tenantId", description = "租户 ID，租户只能看到与其关联的店铺下的菜品", required = true)
    @Operation(summary = "2.5.2 根据关键字或店铺 ID 分页查询菜品信息列表", tags = {"2.5 租户菜品管理"}, operationId = "2.5.2")
    @TokenRequired(role = TokenRequired.ROLE_TENANT)
    @GetMapping("/pageFoodByNameOrStoreId")
    public Result<PaginationResponseVO<TenantFoodDetailVO>> pageFoodByNameOrStoreId(HttpServletRequest request,
                                                                              @RequestParam(required = false) String keyword,
                                                                              @RequestParam(required = false) String storeId,
                                                                              @RequestParam(required = false, defaultValue = "1") int page,
                                                                              @RequestParam(required = false, defaultValue = "10") int size) {
        String tenantId = (String) request.getAttribute("userId");
        log.info("/pageFoodByNameOrStoreId, tenantId = {}, keyword = {}, storeId = {}, page = {}, size = {}", tenantId, keyword, storeId, page, size);
        PaginationResponseVO<TenantFoodDetailVO> vo = tenantFoodManagementService.pageFoodByNameOrStoreId(tenantId, keyword, storeId, page, size);

        log.info("/pageFoodByNameOrStoreId, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }


    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示编辑菜品成功，此时 data 字段为空")
    @ApiResponse(responseCode = "200", description = "code 为 400 时， 表示编辑菜品失败，此时 message 字段为错误提示信息")
    @Operation(summary = "2.5.3 编辑菜品接口", tags = {"2.5 租户菜品管理"}, operationId = "2.5.3")
    @TokenRequired(role = TokenRequired.ROLE_TENANT)
    @PutMapping("/updateFoodById")
    public Result<Void> updateFoodById(@Valid @RequestBody TenantFoodUpdateDTO dto) {
        log.info("/updateFoodById, request: {}", JSONObject.toJSONString(dto));
        tenantFoodManagementService.updateFoodById(dto);
        log.info("/updateFoodById, update successfully.");
        return ResultGenerator.genSuccessResult();
    }


    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示软删除菜品成功，此时 data 字段为空")
    @ApiResponse(responseCode = "200", description = "code 为 400 时， 表示软删除品失败，此时 message 字段为错误提示信息")
    @Parameter(name = "foodId", description = "菜品 ID", example = "698ee133abc5a355ed3ced1607f6eb3c")
    @Operation(summary = "2.5.4 租户软删除菜品接口", tags = {"2.5 租户菜品管理"}, operationId = "2.5.4")
    @TokenRequired(role = TokenRequired.ROLE_TENANT)
    @DeleteMapping("/deleteFoodById")
    public Result<Void> deleteFoodById(@RequestParam String foodId, HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("userId");
        log.info("/deleteFoodById, foodId: {}, tenantId: {}", foodId, tenantId);

        tenantFoodManagementService.deleteFoodByIdForTenant(foodId, tenantId);

        log.info("/deleteFoodById, delete successfully.");
        return ResultGenerator.genSuccessResult();
    }


    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示获取店铺 ID 及菜品分类成功，此时 data 字段为信息列表")
    @Operation(summary = "2.5.5 获取店铺 ID 及菜品分类接口", tags = {"2.5 租户菜品管理"}, operationId = "2.5.5")
    @TokenRequired(role = TokenRequired.ROLE_TENANT)
    @GetMapping("/listStoreAndFoodTypes")
    public Result<List<TenantStoreWithFoodTypeVO>> listStoreAndFoodTypes(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("userId");
        log.info("/listStoreAndFoodTypes, tenantId: {}", tenantId);

        List<TenantStoreWithFoodTypeVO> voList = tenantFoodManagementService.listStoreAndFoodTypes(tenantId);

        log.info("/listStoreAndFoodTypes, response: {}", JSONObject.toJSONString(voList));
        return ResultGenerator.genSuccessResult(voList);
    }


    @ApiResponse(responseCode = "200", description = "code 为 200 时， 表示修改菜品上架状态成功，此时 data 字段为空")
    @ApiResponse(responseCode = "200", description = "code 为 400 时， 表示修改菜品上架状态失败，此时 message 字段为错误提示信息")
    @Operation(summary = "2.5.6 根据店铺 ID 和菜品 ID 修改菜品上架状态", tags = {"2.5 租户菜品管理"}, operationId = "2.5.6")
    @TokenRequired(role = TokenRequired.ROLE_TENANT)
    @PutMapping("/changeFoodSupplyState")
    public Result<Void> changeFoodSupplyState(@Valid @RequestBody TenantChangeFoodSupplyStateDTO dto, HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("userId");
        log.info("/changeFoodSupplyState, tenantId: {}, request：{}", tenantId, JSONObject.toJSONString(dto));

        tenantFoodManagementService.changeFoodSupplyState(dto, tenantId);

        log.info("/changeFoodSupplyState, update food.status successfully.");
        return ResultGenerator.genSuccessResult();
    }


    @ApiResponse(responseCode = "200", description = "code 为 200 时，data 字段为包含菜品信息")
    @Parameter(name = "fooId", description = "菜品 ID", required = true)
    @Operation(summary = "2.5.7 租户根据 ID 查询菜品信息", tags = {"2.5 租户菜品管理"}, operationId = "2.5.7")
    @TokenRequired(role = TokenRequired.ROLE_TENANT)
    @GetMapping("/queryFoodById")
    public Result<TenantFoodVO> queryFoodByIdForTenant(@RequestParam String foodId) {
        log.info("/queryFoodById, foodId: {}", foodId);
        TenantFoodVO foodVO = tenantFoodManagementService.queryFoodById(foodId);

        log.info("/queryFoodById, response: {}", JSONObject.toJSONString(foodVO));
        return ResultGenerator.genSuccessResult(foodVO);
    }

}
